function nmat = createnmat(notes,dur,vel,ch,tempo)
% function nmat = createnmat(notes,dur,vel,ch,tempo)
%
% Create isochronous notematrix
% nmat = createnmat(notes,<dur>,<vel>,<ch>);
% Function creates a notematrix of isochronous pitches based on the NOTES vector.
% This is useful for demonstration purposes and for creating stimuli with certain properties.
%
% Input arguments:
%	notes  [vec] - Pitch vector (e.g. [ 60 64 67] for C major chord)
%	dur    [num] - Note durations in seconds (default 0.25)
%	vel    [num] - Note velocities (0-127, default 100)
%	ch     [num] - Note channel (default 1)
%   tempo  [num] - Tempo in BPM (default 100)
% Output:
%	nmat   [mat] - Notematrix
%
% Remarks: only the notes vector is required for the input, other input 
% arguments are optional and will be replaced by default values if omitted.
%
% Example: Create major scale going up
%   major = [0 2 4 5 7 9 11 12] + 60;
%   nmat = create_nmat(major,0.2,127,1);
%

% Authors:
% Date		Time	Prog	Note
% 26.1.2003	18:44	TE	Created under MATLAB 5.3 (PC)
% Part of the MIDI Toolbox, Copyright 2004, University of Jyvaskyla, Finland
% See License.txt
%
% Modified on 02/17/2010 by Graham Grindlay (grindlay@ee.columbia.edu) to
% add support for tempo
%

% Copyright (C) 2010-2030 Graham Grindlay (grindlay@ee.columbia.edu)
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.

if nargin<1, notes=[0 2 4 5 7 9 11 12] + 60; end % if no arguments at all, create major scale
if nargin<2, dur=0.25; end
if nargin<3, vel=100; end
if nargin<4, ch=1; end
if nargin<5, tempo=100; end

% DEFAULT
if ~isempty(notes)
    if isempty(dur)
        dur=0.25;
        if isempty(vel)
            vel=100;
            if isempty(ch)
                ch=1;
            end
        end 
    end

    % Pitches
    notenro = size(notes,2);
    % Durations
    dur_t = repmat(dur,notenro,1);
    % Velocities
    vel_t = repmat(vel,notenro,1);
    % Channel
    ch_t = repmat(ch,notenro,1);

    onset=zeros(1,notenro)';
    for i = 2:notenro
        onset(i) = onset(i-1)+dur_t(i-1);
    end

    dur_t = repmat(dur,notenro,1);
    notes = notes';

    spb = tempo/60;
    
    onsetb = onset * spb;
    dur_tb = dur_t * spb;

    % onset(beats)	dur(beats)	ch	pitch	velocity	onset(secs)	dur(secs)
    nmat = [onsetb dur_tb ch_t notes vel_t onset dur_t];

else
    disp('Notes needed!')
end
